package com.uuah.server.util.store;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.safehaus.uuid.UUIDGenerator;

import com.uuah.queue.QueuePersistenceStrategy;

public class MemoryPersistenceStrategy implements QueuePersistenceStrategy {

	private UUIDGenerator gen = UUIDGenerator.getInstance();

	private Map map = Collections.synchronizedMap(new HashMap());

	protected Object getId(Object obj) {
		return gen.generateRandomBasedUUID();
	}

	public Object store(String queue, Object obj) throws IOException {
		if (obj == null) {
			throw new IllegalArgumentException("Cannot store null object.");
		}
		Object id = getId(obj);
		map.put(id, obj);
		return id;
	}

	public Object load(String queue, Object id) throws IOException {
		return map.get(id);
	}

	public void remove(String queue, Object id) throws IOException {
		map.remove(id);
	}

	public List restore() throws IOException {
		return new ArrayList();
	}

	public void open() throws IOException {
		// nothing to do
	}

	public void close() throws IOException {
		// nothing to do
	}

	public boolean isTransient() {
		return true;
	}
}
